<?php
namespace app\manage\controller;

use support\Request;
use support\View;
use think\facade\Db;
use app\manage\controller\Base;

class IndexController extends Base
{
    public function index(Request $request)
    {
        // s($request->app);
        return view('index/index');
    }
    public function shou(Request $request)
    {
        if (request()->method()=='POST') {
            $code=$request->input('code','');
            if(!$code){
                return view_e('数据错误');
            }
            $arr=json_decode(jiemi($code),true);
            if(!isset($arr['lizhili']) or $arr['lizhili']!='lizhili'){
                return view_e('授权码错误');
            }else{
                if(!in_array($this->request->rootDomain(),$arr['host'])){
                    return view_e('授权码错误');
                }
            }
            if (file_put_contents(root_path().'tpl/code',$code)) {
                return view_s('授权成功', url('index/index',['st'=>1]));
            } else {
                return view_e('授权失败');
            }
        }

        return view('index/shou');
    }


    //核心页面
    public function main(Request $request)
    {
        View::assign([
            'w' => [
                'zong' => Db::name('article')->whereNull('delete_time')->count(),
                'jin' => Db::name('article')->whereNull('delete_time')->whereDay('create_time')->count(),
                'zuo' => Db::name('article')->whereNull('delete_time')->whereDay('create_time', 'yesterday')->count(),
                'zhou' => Db::name('article')->whereNull('delete_time')->whereWeek('create_time')->count(),
                'yue' => Db::name('article')->whereNull('delete_time')->whereMonth('create_time')->count(),
            ],
            'liu' => [
                'zong' => Db::name('message')->whereNull('delete_time')->count(),
                'jin' => Db::name('message')->whereNull('delete_time')->whereDay('create_time')->count(),
                'zuo' => Db::name('message')->whereNull('delete_time')->whereDay('create_time', 'yesterday')->count(),
                'zhou' => Db::name('message')->whereNull('delete_time')->whereWeek('create_time')->count(),
                'yue' => Db::name('message')->whereNull('delete_time')->whereMonth('create_time')->count(),
            ],
            'yong' => [
                'zong' => Db::name('user')->whereNull('delete_time')->count(),
                'jin' => Db::name('user')->whereNull('delete_time')->whereDay('create_time')->count(),
                'zuo' => Db::name('user')->whereNull('delete_time')->whereDay('create_time', 'yesterday')->count(),
                'zhou' => Db::name('user')->whereNull('delete_time')->whereWeek('create_time')->count(),
                'yue' => Db::name('user')->whereNull('delete_time')->whereMonth('create_time')->count(),
            ],
        ]);

        View::assign('cms', Db::name('cms')->find(1));
        //判断是否为默认密码
        $is_mo = false;
        if ('751aff6be33b5649fde05436dc4cb4f7' == Db::name('admin')->where('id', 1)->value('password')) {
            $is_mo = true;
        }
        View::assign('is_mo', $is_mo);
        return view('index/main');
    }
}
